﻿Ext.define('Wiztop.controller.MeetingTopic', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.data.proxy.JsonP', 'Wiztop.model.Meeting', 'Wiztop.model.Employee'],
    config: {
        refs: {
            meetingtopicview: {
                selector: 'meetingtopicview',
                xtype: 'meetingtopicview',
                autoCreate: true
            },
            meetingTitle: '#meetingTitle',
            btnStopMeeting: '#btnStopMeeting',
            topicList: '#topicList',
            summaryList: '#summaryList',
            btnHistory: '#btnHistory'
        },
        control: {
            btnStopMeeting: {
                tap: 'stopMeeting'
            },
            topicList: {
                itemtap: 'topic_ontap'
            },
            summaryList: {
                itemtap: 'summary_ontap'
            },
            btnHistory: {
                tap: 'history_tap'
            }
        },
        routes: {
            'meetingtopic': 'showMeetingTopicView'
        },
        firstChange: 1
    },
    showMeetingTopicView: function () {
        //切换视图        
        var me = this;
        Ext.Viewport.setActiveItem(this.getMeetingtopicview());
        //显示会议标题
        this.getMeetingTitle().setTitle(sessionStorage.MeetingTitle);

        this.setFirstChange(1);
        sessionStorage.CurrentView = 'meetingtopic';
        sessionStorage.LastView = 'meetingtopic';
        //sessionStorage.setItem("MeetingId", result.ResponseData);
        //右边工具条
        var rightTool = Ext.create('Ext.Container', {
            id: 'topictool',
            top: 0,
            right: 0,
            width: 100,
            autoDestory: true,
            layout: {
                type: 'vbox',
                align: 'center'
            },
            defaults: {
                xtype: 'button',
                width: 100
            },
            items: [
             {
                 text: '进入同步',
                 id: 'btnEnterSync',
                 style: 'margin:10px 0px;',
                 handler: function (e) {
                     if (sessionStorage.Sync == 'false') {
                         e.hide();
                         Ext.Msg.alert('提示', '屏幕同步已结束！', Ext.emptyFn);
                     }
                     else {
                         var ctrl = Wiztop.app.getController('DocDetail');
                         //初始化视图
                         ctrl.showDocDetailView(sessionStorage.ImagePath, sessionStorage.ImageCount, 2, 1, sessionStorage.DocFormat, true);
                         //初始化界面
                         ctrl.initSyncUI();
                         //调转到指定页
                         ctrl.toPage(sessionStorage.PageNumber);
                         //跳转到当前页指定位置
                         ctrl.scrollToPosition(sessionStorage.PositionX, sessionStorage.PositionY);
                     }
                 }
             },
            {
                text: '记事本',
                style: 'margin:10px 0px;',
                handler: function (e) {
                    me.redirectTo('whiteboard/0/path/a/0');
                }
            },
            {
                text: '往期会议',
                style: 'margin:10px 0px;',
                handler: function (e) {
                    //弹出最近十次会议面板
                    var tenPicker = Ext.getCmp('tenPicker');
                    if (tenPicker) {
                        if (tenPicker.getHidden()) {
                            tenPicker.show();
                        }
                        else {
                            tenPicker.hide();
                        }
                    }
                    else {
                        //生成最近十次会议浮动面板
                        tenPicker = Ext.create('Ext.Panel', {
                            id: 'tenPicker',
                            width: 250,
                            height: 400,
                            floating: true,
                            modal: true,
                            hideOnMaskTap: true,
                            style: 'background-color:transparent',
                            layout: {
                                type: 'fit'
                            },
                            items: [
                            {
                                xtype: 'list',
                                itemTpl: '<div style="text-align:center;">{StartDate:date("Y-m-d H:i")}</div>',
                                emptyText: '<div class="emptyText">没有数据</div>',
                                store: new Ext.data.Store({
                                    autoLoad: true,
                                    proxy: {
                                        type: 'jsonp',
                                        model: 'Wiztop.model.Meeting',
                                        url: commLib.Website + '/ashx/MeetingHandler.ashx?method=getlastten&category=' + sessionStorage.MeetingCategory + '&id=' + sessionStorage.MeetingId,
                                        reader: {
                                            type: 'json',
                                            rootProperty: 'ResponseData'
                                        }
                                    }
                                }),
                                listeners: {
                                    itemtap: function (dataview, index, item, record, e) {
                                        //根据新的会议ID，重新加载会议
                                        //暂在历史会议中打开
                                        me.destroyTool();
                                        me.redirectTo('meetinghistorytopic/' + record.raw.Id);
                                    }
                                }
                            }
                        ]
                        }
                        );
                        //显示最近十次会议列表
                        tenPicker.showBy(e);
                    }
                }
            },
            {
                text: '投票',
                style: 'margin:10px 0px;',
                handler: function (e) {
                    me.redirectTo('meetingvote');
                }
            },
            {
                xtype: 'button',
                text: '进入视频',
                ui: 'decline',
                style: 'margin:10px 0px;',
                handler: function () {
                    //alert(sessionStorage.MeetingTitle);
                    //alert(encodeURI(sessionStorage.MeetingTitle));
                    //window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=test@iomeeting.com&hostPass=111111&userName=' + sessionStorage.UserName + '&title=' + encodeURI(sessionStorage.MeetingTitle) + ';http');
                    window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName + '&title=' + encodeURI(sessionStorage.MeetingTitle) + ';http');
                    //                    if (sessionStorage.HostId == sessionStorage.UserId) {
                    //                        //如果是主持人则直接打开视频会议
                    //                        window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName + ';http');
                    //                    }
                    //                    else {
                    //                        //首先检测主持人是否已经开启视频会议
                    //                        Ext.Viewport.mask({ xtype: 'myloadmask', message: '连接视频服务器中' + commLib.loadingText });
                    //                        //直接进入当前最新会议的议题
                    //                        //获取当前最新会议
                    //                        //                    Ext.data.JsonP.request({
                    //                        //                        scope: this,
                    //                        //                        url: commLib.VideoUrl + '/MeetingControl/query?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass,
                    //                        //                        reader: {
                    //                        //                            type: 'text/html'
                    //                        //                        },
                    //                        //                        success: function (result, request) {
                    //                        //                            Ext.Viewport.unmask();
                    //                        //                            var ps = result.split(',');
                    //                        //                            if (ps[0] == 0 || ps[0] == 1 || ps[0] == 2) {
                    //                        //                                Ext.Msg.alert('提示', "主持人还没有启动视频会议，请等待！", Ext.emptyFn);
                    //                        //                            }
                    //                        //                            else if (ps[0] == 3) {
                    //                        //                                //location.href = 'iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName; //Ext.encode(sessionStorage.UserName);
                    //                        //                                window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName);
                    //                        //                            }
                    //                        //                        },
                    //                        //                        failure: function (response) {
                    //                        //                            Ext.Viewport.unmask();
                    //                        //                            Ext.Msg.alert('提示', "视频服务器连接失败！", Ext.emptyFn);
                    //                        //                        }
                    //                        //                    });
                    //                        Ext.Ajax.request({
                    //                            url: commLib.Website + '/ashx/ResponseVideoStatus.ashx?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass,
                    //                            reader: 'text/html',
                    //                            success: function (result, request) {
                    //                                Ext.Viewport.unmask();
                    //                                result = Ext.decode(result.responseText);
                    //                                //alert(result.responseText.success);
                    //                                if (result.success) {
                    //                                    var ps = result.ResponseData.split(',');
                    //                                    if (ps[0] == 0 || ps[0] == 1 || ps[0] == 2) {
                    //                                        Ext.Msg.alert('提示', "主持人还没有启动视频会议，请等待！", Ext.emptyFn);
                    //                                    }
                    //                                    else if (ps[0] == 3) {
                    //                                        //location.href = 'iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName; //Ext.encode(sessionStorage.UserName);
                    //                                        window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName + ';http');
                    //                                    }
                    //                                }
                    //                                else {
                    //                                    Ext.Msg.alert('错误', result.Message, Ext.emptyFn);
                    //                                }
                    //                            },
                    //                            failure: function (response) {
                    //                                Ext.Viewport.unmask();
                    //                                Ext.Msg.alert('错误', "视频服务器连接失败！", Ext.emptyFn);
                    //                            }
                    //                        });
                    //                        //sessionStorage.VideoRoomNumber
                    //                        //location.href = 'iomtouch://http://as.iomeeting11.us?hostEmail=shiyao.xu@iomeeting.com&hostPass=111111&title=123&userName=abc';
                    //                        //location.href = 'iomeeting://' + commLib.VideoUrl + '?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&title=' + sessionStorage.VideoRoomNumber + '&userName=' + sessionStorage.UserName; //Ext.encode(sessionStorage.UserName);                    
                    //                        //alert('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName);
                    //                        //window.open('iomeeting://' + commLib.VideoUrl + '/em.do?hostEmail=' + sessionStorage.HostEmail + '&hostPass=' + sessionStorage.HostPass + '&userName=' + sessionStorage.UserName);
                    //                        //Ext.Msg.alert('提示', "正在调试中,暂未提供正式服务！", Ext.emptyFn);
                    //                    }
                }
            }
            ]
        });

        this.getMeetingtopicview().add(rightTool);
        //根据当前是否为同步状态，显示同步按钮        
        if (sessionStorage.Sync == 'true') {
            Ext.getCmp('btnEnterSync').show();
        }
        else {
            Ext.getCmp('btnEnterSync').hide();
        }

        //在左侧显示参会人员，已到人员，议事规则
        var leftTool = Ext.create('Ext.Container', {
            id: 'leftTool',
            style: 'background-color:transparent',
            top: 0,
            left: 0,
            width: 100,
            autoDestory: true,
            layout: {
                type: 'vbox',
                align: 'center'
            },
            defaults: {
                xtype: 'button'
            },
            items: [
            {
                id: "btnEmployee",
                text: '参会人员',
                style: 'margin:10px 0px',
                handler: function (e) {
                    var employeePicker = Ext.getCmp('employeePicker');
                    if (employeePicker) {
                        if (employeePicker.getHidden()) {
                            employeePicker.show();
                        }
                        else {
                            employeePicker.hide();
                        }
                    }
                    else {
                        //生成参会人员浮动面板
                        employeePicker = Ext.create('Ext.Panel',
                                {
                                    id: 'employeePicker',
                                    width: 250,
                                    height: 400,
                                    floating: true,
                                    modal: true,
                                    hideOnMaskTap: true,
                                    style: 'background-color:transparent',
                                    layout: {
                                        type: 'fit'
                                    },
                                    items: [
                                    {
                                        xtype: 'list',
                                        itemTpl: '<div text-align:center;">{EmployeeName}</div>',
                                        emptyText: '<div class="emptyText">没有数据</div>',
                                        store: new Ext.data.Store({
                                            autoLoad: true,
                                            proxy: {
                                                type: 'jsonp',
                                                model: 'Wiztop.model.Employee',
                                                url: commLib.Website + '/ashx/MeetingHandler.ashx?method=getemployee&id=' + sessionStorage.MeetingId,
                                                reader: {
                                                    type: 'json',
                                                    rootProperty: 'ResponseData'
                                                }
                                            }
                                        })
                                    }]
                                });
                        //显示议题列表
                        employeePicker.showBy(e);
                    }
                }
            },
            {
                id: "btnSignEmployee",
                text: '已到人员',
                style: 'margin:10px 0px;',
                handler: function (e) {
                    var signPicker = Ext.getCmp('signPicker');
                    if (signPicker) {
                        if (signPicker.getHidden()) {
                            signPicker.show();
                        }
                        else {
                            signPicker.hide();
                        }
                    }
                    else {
                        //生成已到人员浮动面板
                        signPicker = new Ext.Panel(
                                {
                                    id: 'signPicker',
                                    floating: true,
                                    modal: true,
                                    hideOnMaskTap: true,
                                    style: 'width:300px;height:400px;background-color:transparent',
                                    layout: {
                                        type: 'fit'
                                    },
                                    items: [
                                    {
                                        xtype: 'dataview',
                                        baseCls: 'meeting',
                                        itemTpl: '<div style="width:35%;text-align:center;">{EmployeeName}</div><div style="width:65%;text-align:center;">{LoginDate:date("Y-m-d H:i")}</div>',
                                        emptyText: '<div class="emptyText">没有数据</div>',
                                        store: new Ext.data.Store({
                                            autoLoad: true,
                                            proxy: {
                                                type: 'jsonp',
                                                model: 'Wiztop.model.Employee',
                                                url: commLib.Website + '/ashx/MeetingHandler.ashx?method=getsignemployee&id=' + sessionStorage.MeetingId,
                                                reader: {
                                                    type: 'json',
                                                    rootProperty: 'ResponseData'
                                                }
                                            }
                                        })
                                    }]
                                });
                        //显示议题列表
                        signPicker.showBy(e);
                    }
                }
            }
            //            ,
            //            {
            //                text: '地块测试',
            //                style: 'margin:10px 0px;',
            //                handler: function (e) {
            //                    var msg = new Ext.MessageBox().show({
            //                        title: '地块文本名称',
            //                        msg: '请输入地块文本名称',
            //                        buttons: Ext.MessageBox.OKCANCEL,
            //                        prompt: { maxlength: 180, autocapitalize: false },
            //                        modal: true,
            //                        fn: function (buttonId, text) {
            //                            if (buttonId == 'ok') {
            //                                if (text == '')
            //                                    text = '4a621007-0b3e-441a-b6ce-6e5cf64edbca';
            //                                //alert('dongguanguotumobileoa://http://192.168.10.12:8084/Upload/plate/201303/92179237-b3b7-4582-9d1f-2d6195b6c280/' + text + '.txt&psnID=' + sessionStorage.UserId + '&psnName=' + sessionStorage.UserName + '&roleName=' + sessionStorage.RoleId + '&psnTitle=' + sessionStorage.UserTitle);
            //                                location.href = 'dongguanguotumobileoa://http://192.168.10.12:8084/Upload/plate/201303/92179237-b3b7-4582-9d1f-2d6195b6c280/' + text + '.txt&psnID=' + sessionStorage.UserId + '&psnName=' + sessionStorage.UserName + '&roleName=' + sessionStorage.RoleId + '&psnTitle=' + sessionStorage.UserTitle;
            //                            }
            //                        },
            //                        icon: Ext.MessageBox.INFO
            //                    });
            //                }
            //            }
            ]
        });
        this.getMeetingtopicview().add(leftTool);
        //获取会议议题
        Ext.data.JsonP.request({
            url: commLib.Website + '/ashx/MeetingHandler.ashx?method=gettopicby&id=' + sessionStorage.MeetingId,
            scope: this,
            success: function (result, request) {
                if (result.success) {
                    //赋值议题数据
                    var store = this.getTopicList().getStore();
                    store.removeAll();
                    store.setData(result.ResponseData.Topics);
                    //赋值会议纪要
                    if (result.ResponseData.Summaries.length == 0) {
                        this.getSummaryList().hide();
                    }
                    else {
                        store = this.getSummaryList().getStore();
                        store.removeAll();
                        store.setData(result.ResponseData.Summaries);
                    }
                    //进入会议之后，每个用户连接远程服务器，等待同步消息
                    connect();
                }
                else {
                    Ext.Msg.alert('错误', result.Message, Ext.emptyFn);
                    return false;
                }
            }
        });
    },
    stopMeeting: function () {
        this.destroyTool();
        this.redirectTo("home");
    },
    topic_ontap: function (dataview, index, item, record, e) {
        this.destroyTool();
        sessionStorage.TopicId = record.raw.TopicId;
        sessionStorage.ReporterId = record.raw.EmployeeId;
        sessionStorage.TopicName = record.raw.TopicTitle;
        //进入议题文档界面
        this.redirectTo('meetingdoc');
    },
    summary_ontap: function (dataview, index, item, record, e) {
        this.destroyTool();
        sessionStorage.DocId = record.raw.Id;
        sessionStorage.ImageCount = record.raw.ImageCount;
        sessionStorage.FilePath = record.raw.Path;
        //进入文档查看界面
        var imagePath = sessionStorage.FilePath + "--" + sessionStorage.MeetingId + "--" + sessionStorage.DocId;
        this.redirectTo('docdetail/' + imagePath + '/' + sessionStorage.ImageCount + "/1/4/" + record.raw.Format);
    },
    history_tap: function () {
        this.redirectTo("meetinghistory");
    },
    destroyTool: function () {
        //如果已到人员面板显示，则注销
        var signPicker = Ext.getCmp('signPicker');
        if (signPicker)
            signPicker.destroy();

        var employeePicker = Ext.getCmp('employeePicker');
        if (employeePicker) {
            employeePicker.destroy();
        }

        var tenPicker = Ext.getCmp('tenPicker');
        if (tenPicker) {
            tenPicker.destroy();
        }


        var topictool = Ext.getCmp('topictool');
        if (topictool) {
            topictool.destroy();
        }
    }
})